/**
 * Created by zhangguanggui on 2017/7/22.
 */
import originJSONP from 'jsonp';

export default function jsonp (url, data, option) {
  // console.log(option);
  let tempUrl = url.indexOf('?') > -1 ? url + '&' : url + '?';
  let fullUrl = tempUrl + para(data);
  return new Promise((resolve, reject) => {
    originJSONP(fullUrl, option, (err, res) => {
      if (err) {
        reject(err);
      } else {
        resolve(res);
      }
    });
  });
}

function para (opt) {
  let url = '';
  for (let k in opt) {
    let v = opt[k] ? opt[k] : '';
    url += `&${k}=${encodeURIComponent(v)}`;
  }
  return url ? url.substring(1) : '';
}
